package com.quarters.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quarters.bean.Notice;
import com.quarters.bean.NoticeOrder;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.request.VoPage;
import com.quarters.commen.BaseController;
import com.quarters.util.OrderIdUtil;
import com.quarters.util.Respons.ResponseUtil;
import com.quarters.util.exception.CommonException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-11
 */
@Controller
@RequestMapping("admin/notice")
public class NoticeController extends BaseController {


    @RequestMapping(value = "notice_add.html",method = RequestMethod.GET)
    public String notice_add(){
        return "admin/notice/notice_add";
    }

    @RequestMapping(value = "notice_edit.html",method = RequestMethod.GET)
    public String notice_edit(Model model, Long id){
        System.out.println("id:"+id);

        model.addAttribute("notice",noticeService.getById(id));
        return "admin/notice/notice_edit";
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Notice> selectMyPage(VoPage voPage, HttpSession session ){
        if(getUserInfo(session).getUsername().equals("admin")){
            return noticeService.selectMyPageAll(voPage);
        }
        return noticeService.selectMyPage(voPage,getUserInfo(session).getId());
    }

    /**
     * @param notice
     * @return 添加
     */
    @RequestMapping(value = "saveNotice",method = RequestMethod.POST)
    @ResponseBody
    public Map saveNotice(@RequestBody Notice notice, HttpSession session){
        notice.setInfoId(getUserInfo(session).getId());
        notice.setType(4);
        if(noticeService.save(notice)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param notice
     * @return 删除
     */
    @RequestMapping(value = "deleteByIdNotice",method = RequestMethod.POST)
    @ResponseBody
    public Map deleteByIdNotice(@RequestBody Notice notice){
        if(noticeService.removeById(notice)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("notice_id",notice.getId());
            NoticeOrder noticeOrder = noticeOrderService.getOne(queryWrapper);
            if(noticeOrderService.removeById(noticeOrder)){
                return ResponseUtil.getSuccessMap();
            }
        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param userId
     * @param content
     *  发送通知
     */
    @RequestMapping(value = "sendNotice",method = RequestMethod.POST)
    @ResponseBody
    public void sendNotice(String userId,String content,HttpSession session,Long noticeId){
        NoticeOrder noticeOrder2 = new NoticeOrder();
        noticeOrder2.setContent(content);
        noticeOrder2.setOrderNumber(OrderIdUtil.gen("CZ",1L));
        noticeOrder2.setCreateTime(System.currentTimeMillis());
        noticeOrder2.setUserList(userId);
        noticeOrder2.setTypeType(3);
        noticeOrder2.setFlage(2);
        noticeOrder2.setType(1);
        noticeOrder2.setNoticeId(noticeId);
        noticeOrder2.setUserId(getUserInfo(session).getId());


        if(getUserInfo(session).getUsername().equals("admin")){
            if(noticeOrderService.save(noticeOrder2)){
                Notice notice = noticeService.getById(noticeId);
                notice.setType(1);
                if(noticeService.updateById(notice)){
                    //是管理员酒发送成功不需要审核支付
//                    articleService.examine(getUserInfo(session).getId(),System.currentTimeMillis(),noticeOrder.getOrderNumber());

                    noticeService.sendNotice(userId,System.currentTimeMillis(),content,noticeId);


                    throw new CommonException("提交成功!");
                }else{
                    throw new CommonException("修改任务状态失败!");
                }
            }else{
                throw new CommonException("推送失败");
            }
        }



        String userIds[] = userId.split(",");
        //如果选择的人数大于两个就需要管理员审核,审核成功每天两次免费,第三次开始收费
        if(userIds.length>1){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            String currentDate = dateFormat.format(new Date());
            List<NoticeOrder> noticeOrderList = noticeOrderService.selectByIdTime(getUserInfo(session).getId(), currentDate);
            System.out.println("通知群发:"+noticeOrderList.size());
            if(noticeOrderList.size()<2){
                noticeOrder2.setWhetherPay(1);
            }else{
                noticeOrder2.setWhetherPay(2);
            }

            if(noticeOrderService.save(noticeOrder2)){
                Notice notice = noticeService.getById(noticeId);
                notice.setType(3);
                if(noticeService.updateById(notice)){
                    articleService.examine(getUserInfo(session).getId(),System.currentTimeMillis(),noticeOrder2.getOrderNumber());
                    throw new CommonException("提交成功,管理员进行审核!");
                }else{
                    throw new CommonException("修改任务状态失败!");
                }
            }
        }else{
            NoticeOrder noticeOrder = new NoticeOrder();
            noticeOrder.setContent(content);
            noticeOrder.setOrderNumber(OrderIdUtil.gen("CZ",1L));
            noticeOrder.setCreateTime(System.currentTimeMillis());
            noticeOrder.setUserList(userId);
            noticeOrder.setTypeType(3);
            noticeOrder.setFlage(2);
            noticeOrder.setWhetherPay(0);
            noticeOrder.setNoticeId(noticeId);
            noticeOrder.setUserId(getUserInfo(session).getId());
            if(noticeOrderService.save(noticeOrder)){
                Notice notice = noticeService.getById(noticeId);
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("notice_id",notice.getId());
                NoticeOrder noticeOrder1 = noticeOrderService.getOne(queryWrapper);
                noticeOrder1.setTypeType(1);
                if(noticeOrderService.updateById(noticeOrder1)){
                    Notice notice1 = noticeService.getById(noticeId);
                    notice1.setType(1);
                    if(noticeService.updateById(notice1)){
                        noticeService.sendNotice(userId,System.currentTimeMillis(),content,noticeId);

                    }else{
                        throw new CommonException("修改任务状态失败");
                    }

                }else{
                    throw new CommonException("修改失败");
                }
            }else{
                throw new CommonException("创建订单失败");
            }
        }


    }

}
